package br.ucb.sacop.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import br.ucb.sacop.R;
import br.ucb.sacop.bean.Turma;
import br.ucb.sacop.bean.TurmaShort;
import br.ucb.sacop.util.TurmaSingleton;

public class AutenticarTurmaActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.autenticar_turma);

		Button btAdd = (Button) findViewById(R.id.botao_add);
		btAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle extras = getIntent().getExtras();
				EditText senha = (EditText) findViewById(R.id.senhaTurma);
				if (extras != null) {
					TurmaShort ts = (TurmaShort) extras.get("turmaShort");
					if (ts.getSenha()
							.equals(senha.getEditableText().toString())) {
						Turma turmaTemp = new Turma();
						turmaTemp.setCodigo(ts.getTurmaId());
						TurmaSingleton.getInstance().add(turmaTemp);
						gerarToast("Turma adicionada, aguardando envio.");
						finish(); // close activity!!!
					} else {
						gerarToast("senha incorreta!");
					}
				} else
					gerarToast("falha ao recuperar turma!");
			}
		});

	}

	private void gerarToast(CharSequence message) {
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast
				.makeText(getApplicationContext(), message, duration);
		toast.show();
	}
}
